App.ProjectModel = Backbone.Model.extend({
	urlRoot : App.root + '/api/projects'
});
App.ProjectCollection = Backbone.Collection.extend({
	model:    App.ProjectModel,
	urlRoot : App.root + '/api/projects'
});

App.ParentModel = Backbone.Model.extend({
	urlRoot : App.root + '/api/parents'
});
App.ParentCollection = Backbone.Collection.extend({
	model   : App.ParentModel,
	url     : App.root + '/api/parents',
	urlRoot : App.root + '/api/parents'
});


App.module('newdialogModule', function(newdialogModule){
	var self = this;
	this.startWithParent = false;
	var _layout = null;
   var MainView = Marionette.ItemView.extend({
		tagName: 'div',
		events: {
			'click #new-dialog-submit': 'submit'
		},
		submit: function() {
			if (App.debug) console.log(self.moduleName + ' MainView::submit invoked');
			self.Controller.save();
		}
	});
	var Controller = Marionette.Controller.extend({
		initialize: function(options){
			if (App.debug) console.log(this.moduleName + ' Controller initialized');
			this.options = options;
			this.projects = new App.ProjectCollection;
			this.parents  = new App.ParentCollection;
		},
		show: function() {
			if (App.debug) console.log(self.moduleName + ' Controller show invoked');
         console.log(this.options);
			var main_view = new MainView({
				id: this.options.id,
				template: '#' + this.options.template
			});
			this.options.region.show(main_view);
		},
		refresh: function() {
			if (App.debug) console.log(self.moduleName + ' Controller refresh invoked');
			//var Parents = new App.ParentCollection;
			this.parents.fetch({async: false});
			var options = '<option value="">Sin Antecesor</option>';
			this.parents.each(function(e){
				options = options + '<option value="' + e.attributes.id +'" >' + e.attributes.nombre + '</option>';
				$('#data-predecessor').html(options);
			});
			//$("#data-predecessor").wijdropdown();
		},
		render: function() {
			this.parents.fetch({async: false});
			this.refresh();

			if (App.debug) console.log(self.moduleName + ' Controller render invoked');
			$('#new-dialog').wijdialog({});
			$(".form-input").wijtextbox();
			$(".date-input").wijinputdate();
			$("#data-activity").wijcheckbox();
			$("#new-dialog-submit").button();
		},
		save: function() {
			console.log($($('#data-activity').widget()[0]).parent().parent().hasClass('ui-state-checked'));
			
			
			var that = this;
			if (App.debug) console.log(self.moduleName + ' Controller save invoked');
			if (App.debug) console.log('data-name:'          + $('#data-name').val());
			if (App.debug) console.log('data-predecessor:'   + $('#data-predecessor').val());
			if (App.debug) console.log('data-pr-begin-date:' + $('#data-pr-begin-date').val());
			if (App.debug) console.log('data-pr-end-date:'   + $('#data-pr-end-date').val());
			if (App.debug) console.log('data-activity:'      + $($('#data-activity').widget()[0]).parent().parent().hasClass('ui-state-checked'));
			var project = new App.ProjectModel();
			project.save(
				{
					nombre:					$('#data-name').val(),
					antecesor:				$('#data-predecessor').val(),
					inicio_programado:   $('#data-pr-begin-date').val(),
					fin_programado:      $('#data-pr-end-date').val(),
					actividad:				($($('#data-activity').widget()[0]).parent().parent().hasClass('ui-state-checked'))?1:0				
				},
				{
					success: function() {
						console.log('saved');
						that.refresh()
					}
				}
			);
			
			/*
			this.projects.create([
				{
					nombre:					$('#data-name').val(),
					antecesor:				$('#data-predecessor').val(),
					inicio_programado:   $('#data-pr-begin-date').val(),
					fin_programado:      $('#data-pr-end-date').val(),
					actividad:				$('#data-activity').attr('rel')
				},
				{
					success: function() {
						console.log('saved');
						this.refresh()
					}
				}
			]);
			*/
			//this.refresh();
		}
	});
	newdialogModule.addInitializer(function (options) {
		var defaults = {
         id:                         'new-dialog',
         template:                   'app-new-dialog-template'
		};
      options = _.extend(defaults, options);
		if (App.debug) console.log('App.dialogModule addInitializer invoked');
		newdialogModule.Controller = new Controller(options);
		newdialogModule.Controller.show();
		newdialogModule.Controller.render();
   });
});

App.newdialogModule.on("before:start", function(){
	if (App.debug) console.log(this.moduleName + ' before:start');
});

App.newdialogModule.on("start", function(){
	if (App.debug) console.log(this.moduleName + ' start');
});

